<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class Village extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_village';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'check_status_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }
    public function getCheckStatusList()
    {
        return ['0' => __('Check_status 0'), '1' => __('Check_status 1'), '2' => __('Check_status 2')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getCheckStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['check_status']) ? $data['check_status'] : '');
        $list = $this->getCheckStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    // 处理信息
    public function getNearbyBusJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_bus_json']) ? $data['nearby_bus_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbyHospitalJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_hospital_json']) ? $data['nearby_hospital_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbySubwayJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_subway_json']) ? $data['nearby_subway_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbySchoolJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_school_json']) ? $data['nearby_school_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }
    // 处理信息
    public function getNearbyShopJsonArrAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['nearby_shop_json']) ? $data['nearby_shop_json'] : '');
        $arr = [];
        if ($value) {
            $arr = json_decode($value, true);
        }
        return $arr;
    }

    /**
     * 租房
     */
    public function rental(){
        return $this->hasMany("Rental",'village_id','id');
    }
}
